//会员id
var mid;
//礼品数量
var preNum;
//选择数量
var resNum;
//礼品id
var preid;
//可用积分
var integral;
//礼品积分
var preintegral;

$(function () {
    $("#editForm").form({
        onSubmit: function (param) {
            param.mid = mid;
            param.preid = preid;
            param.prenum = resNum;
        }
    })

    //会员列表初始化
    $("#member_datagrid").datagrid({
        fit: true,
        fitColumns: false,
        striped: true,
        url: "/member/list.do",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: "#tb",
        columns: [[
            {field: "cb", checkbox: true},
            {field: "id", hidden: true},
            {field: "cardNum", title: "卡号", width: 75},
            {field: "name", title: "会员名称", width: 75},
            {field: "name", title: "分组", width: 75},
            {field: "name", title: "会员等级", width: 75},
            {field: "integral", title: "可用积分", width: 75},
            {field: "balance", title: "储值余额", width: 75}
        ]], onClickRow: function (index, row) {
            $.get("/member/getMemberByExchangeId.do?id=" + row.id, function (data) {
                $("#editForm").form("load", data[0]);
                integral = data[0].integral;
                //会员id
                console.log(data[0]);
                mid = data[0].id;
                //可用积分
                integral = data[0].integral;
            })
        }
    })
    //end

    //礼品列表初始化
    $("#pre_datagrid").datagrid({
        fit: true,
        fitColumns: false,
        striped: true,
        url: "/present/list.do",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: "#tb",
        columns: [[
            {field: "cb", checkbox: true},
            {field: "id", hidden: true},
            {field: "sn", title: "礼品编号", width: 75},
            {field: "prename", title: "礼品名称", width: 115},
            {field: "preintegral", title: "兑换积分", width: 95},
            {field: "totalNum", title: "礼品数量", width: 95},
            {field: "unit", title: "礼品单位", width: 95}
        ]], onClickRow: function (index, row) {
            $.get("/present/getPresent.do?id=" + row.id, function (data) {
                $("#editForm").form("load", data);
                // preintegral = data.preintegral;
                preintegral = data.preintegral;
                var result = integral - preintegral + "";
                $("#balance").val(result);
                //礼品id
                preid = data.id;
                //礼品积分
                preintegral = data.preintegral;
                var prenum = data.totalNum - 1 + "";
                preNum = data.totalNum - prenum + "";
                resNum = prenum;
                $("#preNum").val(preNum);
            })
        }
    })
    //end


    // $("#exchange").click(function () {
    //     if (preintegral > integral) {
    //         $.messager.alert("温馨提示", "亲,积分不足呦");
    //         return;
    //     }
    //     if (preintegral == null) {
    //         $.messager.alert("温馨提示", "亲,请选择要兑换的礼品");
    //         return;
    //     }
    //     // $.messager.confirm("温馨提示", "亲,确认兑换吗?", function (r) {
    //     //     if (r) {
    //     //         $.get("/recordExchange/save.do", function (data) {
    //     //             if (data.success) {
    //     //                 //提示成功
    //     //                 $.messager.alert('温馨提示', "恭喜亲,兑换成功啦");
    //     //                 //刷新数据
    //     //                 $("#member_datagrid").datagrid("load");
    //     //                 $("#editForm").form("clear");
    //     //                 $("#pre_datagrid").datagrid("load");
    //     //                 //关闭窗口
    //     //                 $("#ex_dialog").dialog("close");
    //     //
    //     //             } else {
    //     //                 $.messager.alert("温馨提示", data.msg, 'warning');
    //     //             }
    //     //         })
    //     //     }
    //     // })
    // })

})

function exchange() {
    console.log(preintegral);
    console.log(integral);
    if (preintegral > integral) {
        $.messager.alert("温馨提示", "亲,积分不足呦");
        return;
    }
    if (preintegral == null) {
        $.messager.alert("温馨提示", "亲,请选择要兑换的礼品");
        return;
    }
    //表单提交url
    $("#editForm").form("submit", {
        url: "/recordExchange/save.do",
        success: function (data) {
            data = $.parseJSON(data);
            console.log(data);
            if (data.success) {
                //提示成功
                $.messager.confirm('温馨提示', "恭喜亲,兑换成功啦", function (r) {
                    //关闭窗口
                    //刷新数据
                    $("#member_datagrid").datagrid("load");
                    $("#pre_datagrid").datagrid("load");
                    // $("#ex_dialog").dialog("close");
                });
            } else {
                $.messager.alert('温馨提示', data.msg, "error");
            }
        }
    })
}
//重置(刷新)
function reset() {
    $("#editForm").form("clear");
}